#include "ndk_jni_Speaker.h"
#include <android/log.h>
#include <cstdlib>
#include "UUOO.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef LOG_H__
#define LOG_TAG "cpp-log"
#define LOGW(a)  __android_log_write(ANDROID_LOG_WARN,LOG_TAG,a)
#define LOGFMTW(fmt,a)  __android_log_print(ANDROID_LOG_WARN,LOG_TAG,fmt,a)
#endif

//定义了没有释放，释放用    delete uo;
UUOO* uo = new UUOO();

JNIEXPORT jstring JNICALL Java_ndk_jni_Speaker_getTalk (JNIEnv *env, jobject obj){


	LOGFMTW("%d",uo->getK());

	return env->NewStringUTF("Hello World! zzzzz");
}

JNIEXPORT void JNICALL Java_ndk_jni_Speaker_sendNativeString (JNIEnv *env, jobject obj, jstring jstr){

	jboolean *b = new unsigned char('0');
	const char *str = env->GetStringUTFChars(jstr,b);

	if(str != NULL){
		LOGW(str);
	}

	env->ReleaseStringUTFChars(jstr,str);

}


JNIEXPORT void JNICALL Java_ndk_jni_Speaker_sendNativeInteger(JNIEnv *env, jobject obj, jint obj0){
	LOGFMTW("%d",obj0);
}

JNIEXPORT void JNICALL Java_ndk_jni_Speaker_sendNativeBoolean(JNIEnv *env, jobject obj, jboolean obj0){
	if(obj0){
		LOGW("it's true ,I come here!");
	}else{
		LOGW("it's false ,I come here!");
	}
}

/*
 * Class:     ndk_jni_Speaker
 * Method:    getAndChangeJavaIntField
 * Signature: (Lndk/jni/User;)V
 */
JNIEXPORT void JNICALL Java_ndk_jni_Speaker_getAndChangeJavaField(JNIEnv *env, jobject testObj, jobject userObj){
	uo->getAndChangeJavaField(env,testObj,userObj);
}

JNIEXPORT void JNICALL Java_ndk_jni_Speaker_callJavaMethod(JNIEnv *env, jobject testObj, jobject userObj){
	uo->callJavaMethod(env,testObj,userObj);
}


JNIEXPORT jstring JNICALL Java_ndk_jni_Speaker_getEmmcStorageSize(JNIEnv* env, jclass jobj) {

	int buffer_size = 512;
	char* buffer = (char*)malloc(sizeof(char)*buffer_size);
	FILE* fp = popen("df", "r");
	memset(buffer,0,buffer_size);
	char* s = NULL;
	while ( (s = fgets(buffer, buffer_size, fp)) != NULL) {
		LOGW(buffer);
		memset(buffer,0,buffer_size);
	}
	pclose(fp);
	free(buffer);
	jstring js = env->NewStringUTF("ddd");

	return js;
}

